package com.delicious.core.base;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

@Data
@AllArgsConstructor
public class BaseResponse<T> implements Serializable {

    /**
     * 返回状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T result;

    public static <T> BaseResponse<T> of(T data) {
        return new BaseResponse(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), data);
    }

    public static BaseResponse success() {
        return new BaseResponse(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), null);
    }

    public static BaseResponse error(ResponseEnum code) {
        return new BaseResponse(code.getCode(), code.getMsg(), null);
    }

    public static <T> BaseResponse<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return error(ResponseEnum.FAILED);
        }
    }

}